//
//  CreateTaskViewController.m
//  test
//
//  Created by liao Echo on 14-5-14.
//  Copyright (c) 2014年 Echo. All rights reserved.
//

#import "CreateTaskViewController.h"
#import "NewTaskViewController.h"
#import "DataManager.h"
#import "PokerControlClient.h"

@interface CreateTaskViewController ()

@end

@implementation CreateTaskViewController
@synthesize m_savaDevice,strName;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = STR(@"WI-FI智能插座");
    if ([Global curLanguageEn]) {
        self.title = @"Smart Socket";
    }
    [[PokerControlClient share] startCmd:@"11" code:nil];
    
    
//    [[PokerControlClient share] startCmd:@"19" code:nil];//返回手机开关、延时及定时列表
    
     array = [[NSMutableArray alloc] init];
    self.strName = [NSString stringWithFormat:@"%@_%@", m_savaDevice, SOCKET_DEVICE_TIME_LIST];
    
    
//    NSArray *deviceTimeList = [UD objectForKey:self.strName];
//    [array addObjectsFromArray:deviceTimeList];
    
    
    NSString *path = [DataManager storePathWithFileName:self.strName];
    [array addObjectsFromArray:[DataManager unarchiverObjectFromPath:path key:self.strName]];
    
    
    TableView1 = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-44-20) style:UITableViewStylePlain];
    TableView1.backgroundColor = [UIColor clearColor];
    TableView1.delegate = self;
    TableView1.dataSource = self;
    TableView1.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:TableView1];
    
    UIImageView *imageview = [[UIImageView alloc] initWithImage:IMG(@"socket_task_add_bg.png")];
    imageview.userInteractionEnabled = YES;
    imageview.frame = CGRectMake(0, 0, SCREEN_WIDTH, 32+8);
    TableView1.tableHeaderView = imageview;
    
    UIImageView *leftLogo = [[UIImageView alloc] initWithImage:IMG(@"socket_task_add_left.png")];
    leftLogo.frame = CGRectMake(15, 7, 25, 25);
    [imageview addSubview:leftLogo];
    
    GoodLabel *label = [GoodLabel labelWithFont:12 title:STR(@"创建任务")];
    if ([Global curLanguageEn]) {
        label.text = @"Create Task";
    }
    label.frame = CGRectMake(FX(leftLogo)+FW(leftLogo)+5, 0, 100, FH(imageview));
    [imageview addSubview:label];
    
    UIImageView *rightLogo = [[UIImageView alloc] initWithImage:IMG(@"socket_task_add_right.png")];
    rightLogo.frame = CGRectMake(SCREEN_WIDTH-25-20, 6, 25, 25);
    [imageview addSubview:rightLogo];
    
    GoodBtn *btn = [GoodBtn btnWithFont:1];
    btn.frame = CGRectMake(0, 0, SCREEN_WIDTH, FH(imageview));
    [imageview addSubview:btn];
    
    [btn addTarget:self action:@selector(newTaskPressed) forControlEvents:UIControlEventTouchUpInside];
}
-(void)newTaskPressed{
    NewTaskViewController *view = [[NewTaskViewController alloc] init];
    view.delegate = self;
    [self.navigationController pushViewController:view animated:YES];
}
-(void)newTaskType:(SocketInfo*)socketInfo{
    [array addObject:socketInfo];
    
    NSString *path = [DataManager storePathWithFileName:self.strName];
    [DataManager archiverObject:array path:path key:self.strName];
    
//    [UD setObject:array forKey:self.strName];
//    [UD synchronize];
    [TableView1 reloadData];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return array.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 64;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"TableView1Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    UIImageView *bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    bg.image = IMG(@"socket_on_off_bg.png");
    [cell addSubview:bg];
    
    UIImageView *logo = [[UIImageView alloc] initWithImage:IMG(@"icon_socket.png")];
    logo.frame = CGRectMake(1.5+8+8, 7, 55/1.8, 84/1.8);
    [cell addSubview:logo];
    
    SocketInfo *socketInfo = [array objectAtIndex:indexPath.row];
    
    GoodLabel *label = [GoodLabel labelWithFont:14 title:socketInfo.time];
    label.textColor = [UIColor blackColor];
    label.frame = CGRectMake(FX(logo)+FW(logo)+20, 0, 100, 64);
    [cell addSubview:label];
    
    label = [GoodLabel labelWithFont:14 title:STR(@"智能定时插座")];
    if ([Global curLanguageEn]) {
        label.text = @"Fixed Socket";
    }
    label.textColor = [UIColor whiteColor];
    label.frame = CGRectMake(FX(logo)+FW(logo)+20+50+20, 5, 100, 30);
    [cell addSubview:label];
    if (socketInfo.type == 0) {
        for (int i=0; i<7; i++) {
            GoodLabel *label1 = [GoodLabel labelWithFont:10 title:@""];
            label1.textAlignment = UITextAlignmentCenter;
            label1.textColor = [UIColor blackColor];
            label1.frame = CGRectMake(FX(label)+i*10, 35, 10, 20);
            [cell addSubview:label1];
            if (i == 0) {
                label1.text = @"日";
                if (socketInfo.sunday_able) {
                    label1.textColor = [UIColor whiteColor];
                }
                if ([Global curLanguageEn]) {
                    label1.text = @"S";
                }
            }else if (i == 1) {
                label1.text = @"一";
                if (socketInfo.monday_able) {
                    label1.textColor = [UIColor whiteColor];
                }
                if ([Global curLanguageEn]) {
                    label1.text = @"M";
                }
            }else if (i == 2) {
                label1.text = @"二";
                if (socketInfo.tuesday_able) {
                    label1.textColor = [UIColor whiteColor];
                }
                if ([Global curLanguageEn]) {
                    label1.text = @"T";
                }
            }else if (i == 3) {
                label1.text = @"三";
                if (socketInfo.wednesday_able) {
                    label1.textColor = [UIColor whiteColor];
                }
                if ([Global curLanguageEn]) {
                    label1.text = @"W";
                }
            }else if (i == 4) {
                label1.text = @"四";
                if (socketInfo.thursday_able) {
                    label1.textColor = [UIColor whiteColor];
                }
                if ([Global curLanguageEn]) {
                    label1.text = @"T";
                }
            }else if (i == 5) {
                label1.text = @"五";
                if (socketInfo.friday_able) {
                    label1.textColor = [UIColor whiteColor];
                }
                if ([Global curLanguageEn]) {
                    label1.text = @"F";
                }
            }else if (i == 6) {
                label1.text = @"六";
                if (socketInfo.saturday_able) {
                    label1.textColor = [UIColor whiteColor];
                }
                if ([Global curLanguageEn]) {
                    label1.text = @"S";
                }
            }
        }
    }
    else if (socketInfo.type == 1) {
        label.text = STR(@"智能延时插座");
        if ([Global curLanguageEn]) {
            label.text = @"Delay Socket";
        }
    }
    
    UIImageView *rightlogo = [[UIImageView alloc] initWithImage:IMG(@"button_off.png")];
    rightlogo.frame = CGRectMake(SCREEN_WIDTH-91/1.8-5, (64-35/1.8)/2, 91/1.8, 35/1.8);
    [cell addSubview:rightlogo];
    
    if (socketInfo.onState) {
        rightlogo.image = IMG(@"button_on.png");
    }
    
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
	if (array.count>0) {
		return UITableViewCellEditingStyleDelete;
	}else {
		return UITableViewCellEditingStyleNone;
	}
}

#pragma mark -
#pragma mark Table View Data Source Methods
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (array.count > indexPath.row) {
        [array removeObjectAtIndex:indexPath.row];
    }
//    [UD setObject:array forKey:self.strName];
//    [UD synchronize];
    
    NSString *path = [DataManager storePathWithFileName:self.strName];
    [DataManager archiverObject:array path:path key:self.strName];
    
    if ([array count] == 0) {
        [tableView reloadData];
    } else {
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}
@end
